@section('title', '编辑管理员')
@section('id', $id)
@section('content')
        <style type="text/css">
            .third_quote{display: inline-block; font-weight: bold; height: 30px; line-height: 30px;}
        </style>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称：</label>
            <div class="layui-input-block">
                <input type="text" value="{$info.role_name ?? ''}" name="role_name" required  placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色说明：</label>
            <div class="layui-input-block">
                <textarea name="role_intro" placeholder="请输入角色说明" class="layui-textarea" required >{$info.role_intro ?? ''}</textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限列表：</label>
            <div class="layui-input-block menu">
                <input type="checkbox" name="mAllChoose" lay-skin="primary" lay-filter="mAllChoose" title="全选">
                {foreach $menu as $m}
                    <div  style="width: 100%">
                    <input type="checkbox" {if in_array($m['id'], $info['menu_ids'])}checked{/if} name="menu_list[]" value="{$m['id']}" lay-filter="mainChoose" lay-skin="primary" title="{$m['title']}">
                    </div>
                    {if isset($m['children'])}
                    <div  style="padding-left: 20px; width: 100%">
                        {foreach $m['children'] as $mc}
                        <p><input type="checkbox" {if in_array($mc['id'], $info['menu_ids'])}checked{/if} name="menu_list[]" value="{$mc['id']}" lay-filter="mainChoose" parent_id="{$mc['parent_id']}" lay-skin="primary" title="{$mc['title']}"></p>

                        {if isset($mc['children'])}
                            <div style="padding-left: 40px; width: 100%;">
                                {foreach $mc['children'] as $three}
                                <input type="checkbox" {if in_array($three['id'], $info['menu_ids'])}checked{/if} name="menu_list[]" value="{$three['id']}" parent_id="{$three['parent_id']}" lay-skin="primary" title="{$three['title']}">
                                {/foreach}
                            </div>
                        {/if}

                        {/foreach}
                    </div>
                    {/if}

                {/foreach}
            </div>
        </div>
@endsection


@section('js')
<script>
    layui.use(['form','jquery','laypage', 'layer'], function() {
        var form = layui.form,
            $ = layui.jquery;
        form.render();
        var layer = layui.layer;
        form.verify({
        //    title: [/[\u4e00-\u9fa5]{2,12}$/, '标题必须2到12位汉字'],
        //    intro: [/[\u4e00-\u9fa5]{2,30}$/, '权限介绍必须2到30位汉字'],
        });

        var child = $(".menu").find('input[type="checkbox"]');
        //form.render('checkbox');

        //设置子菜单选中
        form.on('checkbox(mainChoose)', function(data) {
            var obj = $("input[type='checkbox'][parent_id='"+ data.value +"']");
             obj.each(function(index, item) {
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });

        form.on('checkbox(mAllChoose)', function(data) {
            var child = $(".menu").find('input[type="checkbox"]');
            child.each(function(index, item) {
                item.checked = data.elem.checked;
            });
            if(data.elem.checked)$(this).attr('title','全不选');
            else $(this).attr('title','全选');
            form.render('checkbox');
        });


        form.on('submit(formDemo)', function(data) {
            $.ajax({
                url:" {{url('save')}}",
                data:$('form').serialize(),
                type:'post',
                dataType:'json',
                success:function(res){
                    if(res.code == 200){
                        layer.msg(res.message,{icon:6});
                        var index = parent.layer.getFrameIndex(window.name);
                        setTimeout('parent.layer.close('+index+')',1000);
                    }else{
                        layer.msg(res.message,{shift: 6,icon:5});
                    }
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败', {time: 1000});
                }
            });
            return false;
        });
    });
</script>
@endsection
@extends('admin.common.edit')